package com.tigercar.servicemanagement.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tigercar.common.pojo.PageResult;
import com.tigercar.common.utils.BeanHelper;
import com.tigercar.servicemanagement.dto.EmployeeDTO;
import com.tigercar.servicemanagement.mapper.EmployeeMapper;
import com.tigercar.servicemanagement.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class EmployeeService {

    @Autowired(required = false)
    private EmployeeMapper employeeMapper;

    public PageResult<EmployeeDTO> findEmployee(Integer page, Integer rows, String key) {

        //分页参数
        IPage<Employee> iPage = new Page<>(page,rows);

        //查询条件
        QueryWrapper<Employee> queryWrapper = Wrappers.query();

        //处理条件
        if (StringUtils.isNotEmpty(key)){

            queryWrapper.and(
                    i->i.like("employee_name",key)
                    .or()
                    .like("phone",key)
                    .or()
                    .like("email",key)
                    .or()
                    .like("dept_name",key)
                    .or()
                    .like("state",key)
            );
        }

        iPage = employeeMapper.selectPage(iPage,queryWrapper);

        //处理结果，返回结果
        List<Employee> employeeList = iPage.getRecords();
        List<EmployeeDTO> employeeDTOList = BeanHelper.copyWithCollection(employeeList,EmployeeDTO.class);
        getResult(employeeDTOList);
        PageResult<EmployeeDTO> pageResult = new PageResult<>(iPage.getTotal(),iPage.getPages(),employeeDTOList);

        return pageResult;
    }

    /**
     * 封装返回结果
     * @param employeeDTOList
     */
    private void getResult(List<EmployeeDTO> employeeDTOList) {

        employeeDTOList.forEach(employeeDTO -> {
            Employee employee = this.findEmployeeById(employeeDTO.getId());
            employeeDTO.setEmployeename(employee.getEmployeename());
            employeeDTO.setPhone(employee.getPhone());
            employeeDTO.setEmail(employee.getEmail());
            employeeDTO.setDeptname(employee.getDeptname());
        });
    }

    public Employee findEmployeeById(Integer id) {
        Employee employee = employeeMapper.selectById(id);
        return employee;
    }

    public void addEmployee(Employee employee, List<String> deptname) {
        try {
            employeeMapper.insert(employee);

            employeeMapper.saveEmployee(employee.getId(),deptname);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void updateEmployee(Employee employee, List<String> deptname) {

        try {
            Employee newEmployee = new Employee();
            newEmployee.setUsername(employee.getUsername());
            newEmployee.setDeptname(employee.getDeptname());
            newEmployee.setPhone(employee.getPhone());
            newEmployee.setEmail(employee.getEmail());
            employeeMapper.updateEmployee(newEmployee.getId(),deptname);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
